package com.ye.personnel.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ye.personnel.api.enums.FlowTypeEnum;
import com.ye.personnel.api.enums.UserTypeEnum;
import com.ye.personnel.api.flow.*;
import com.ye.personnel.common.exception.CustomerException;
import com.ye.personnel.common.page.BasePageParam;
import com.ye.personnel.common.page.PageModel;
import com.ye.personnel.common.response.ErrorCode;
import com.ye.personnel.common.utils.JwtUtil;
import com.ye.personnel.mapper.dao.*;
import com.ye.personnel.mapper.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: yeubo
 * @Date: 2024/1/2 15:57
 */
@Service
@Slf4j
public class FlowService {

    @Autowired
    GwyDepartmentMapper departmentMapper;
    @Autowired
    GwyAdminUserMapper adminUserMapper;
    @Autowired
    GwyStartFlowMapper startFlowMapper;
    @Autowired
    GwyFlowPersonnelMapper flowPersonnelMapper;
    @Autowired
    GwyFlowAttachmentMapper flowAttachmentMapper;
    @Autowired
    GwyFlowHistoryMapper flowHistoryMapper;
    @Autowired
    GwyFlowNoticeMapper flowNoticeMapper;
    @Autowired
    PersonnelMapper personnelMapper;

    /**
     * 流程列表
     * @param params
     * @return
     */
    public PageModel<FlowListVo> flowList(FlowListSearch params){

        String loginId = JwtUtil.getLoginId();
        GwyAdminUser adminUser = adminUserMapper.selectById(loginId);
        if(adminUser == null){
            throw new CustomerException(ErrorCode.TOKEN_INVALID,"登录失效，请重新登录");
        }
        params.setAdminStatus(adminUser.getAdminStatus());
        String departmentId = adminUser.getDepartmentId();
        params.setDepartmentId(departmentId);
        Integer userType = adminUser.getUserType();
        List<Integer> flowTypeByUserType = getFlowTypeByUserType(userType);
        params.setFlowTypeList(flowTypeByUserType);
        Page<FlowListVo> rowPage = new Page<>(params.getPage(), params.getPageSize());
        Page<FlowListVo> flowListVoPage = personnelMapper.flowList(rowPage, params);
        List<FlowListVo> records = flowListVoPage.getRecords();
        if(CollectionUtils.isEmpty(records)){
            return new PageModel<>(flowListVoPage, Collections.emptyList());
        }
        for (FlowListVo flowListVo : records){
            Integer currentCheckType = flowListVo.getCurrentCheckType();
            if(currentCheckType == 0 && flowListVo.getCurrentUserType().equals(userType)){
                flowListVo.setCheckAllow(1);
            }else if(currentCheckType == 1 && Objects.equals(flowListVo.getCurrentDepartmentId(), departmentId)){
                flowListVo.setCheckAllow(1);
            }
            //发起单位有编辑权限
            if(flowListVo.getFlowPos() == 0 && flowListVo.getCheckAllow() == 1){
                flowListVo.setEditAllow(1);
            }
            flowListVo.setFlowTypeName(FlowTypeEnum.getName(flowListVo.getFlowType()));
            List<FlowPersonnelListVo> flowPersonnelListVos = personnelMapper.flowPersonnelList(flowListVo.getFlowId());
            //人员列表
            if(CollectionUtils.isNotEmpty(flowPersonnelListVos)){
                List<String> personnelNameList = flowPersonnelListVos.stream()
                        .map(FlowPersonnelListVo::getPersonnelName).collect(Collectors.toList());
                flowListVo.setPersonnelNameList(personnelNameList);
            }
        }

        return new PageModel<>(flowListVoPage,records);
    }

    /**
     * 流程提醒列表
     * @param params
     * @return
     */
    public PageModel<FlowNoticeListVo> flowNoticeList(BasePageParam params){
        String loginId = JwtUtil.getLoginId();
        GwyAdminUser adminUser = adminUserMapper.selectById(loginId);
        if(adminUser == null){
            throw new CustomerException(ErrorCode.TOKEN_INVALID,"登录失效，请重新登录");
        }
        String departmentId = adminUser.getDepartmentId();
        Page<GwyFlowNotice> rowPage = new Page<>(params.getPage(), params.getPageSize());
        LambdaQueryWrapper<GwyFlowNotice> queryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(params.getSearchBody())){
            queryWrapper.like(GwyFlowNotice::getNoticeTitle,params.getSearchBody());
        }
        if(adminUser.getAdminStatus() != 1){
            Integer userType = adminUser.getUserType();
            List<Integer> flowTypeByUserType = getFlowTypeByUserType(userType);
            queryWrapper.and(i -> i.in(GwyFlowNotice::getFlowType, flowTypeByUserType).or()
                    .eq(GwyFlowNotice::getStartDepartmentId, departmentId).or()
                    .eq(GwyFlowNotice::getTargetDepartmentId, departmentId));
        }
        queryWrapper.orderByDesc(GwyFlowNotice::getCreatedTime);
        Page<GwyFlowNotice> flowNoticePage = flowNoticeMapper.selectPage(rowPage, queryWrapper);
        List<GwyFlowNotice> records = flowNoticePage.getRecords();
        if(CollectionUtils.isEmpty(records)){
            return new PageModel<>(flowNoticePage, Collections.emptyList());
        }
        List<FlowNoticeListVo> flowNoticeListVos = BeanUtil.copyToList(records, FlowNoticeListVo.class);
        return new PageModel<>(flowNoticePage,flowNoticeListVos);
    }

    /**
     * 流程详情
     * @param flowId
     * @return
     */
    public FlowDetailVo flowDetail(String flowId){
        GwyStartFlow startFlow = startFlowMapper.selectById(flowId);
        if(startFlow == null) {
            throw new CustomerException(ErrorCode.PARAMS_GET_ERROR, "流程不存在");
        }
        String loginId = JwtUtil.getLoginId();
        GwyAdminUser adminUser = adminUserMapper.selectById(loginId);
        if(adminUser == null){
            throw new CustomerException(ErrorCode.TOKEN_INVALID,"登录失效，请重新登录");
        }
        List<Integer> flowTypeByUserType = getFlowTypeByUserType(adminUser.getUserType());
        if(adminUser.getAdminStatus() == 0 && !flowTypeByUserType.contains(startFlow.getFlowType())
                && !Objects.equals(adminUser.getDepartmentId(), startFlow.getStartDepartmentId())
                && !Objects.equals(adminUser.getDepartmentId(), startFlow.getTargetDepartmentId()) ){
            throw new CustomerException(ErrorCode.PARAMS_GET_ERROR, "无权限查看此流程");
        }
        FlowDetailVo flowDetailVo = BeanUtil.copyProperties(startFlow, FlowDetailVo.class);
        GwyDepartment startDepartment = departmentMapper.selectById(flowDetailVo.getStartDepartmentId());
        flowDetailVo.setStartDepartmentName(startDepartment != null ? startDepartment.getDepartmentName() : "");
        flowDetailVo.setFlowTypeName(FlowTypeEnum.getName(flowDetailVo.getFlowType()));
        //流程人员列表
        List<FlowPersonnelListVo> flowPersonnelListVos = personnelMapper.flowPersonnelList(flowId);
        flowDetailVo.setFlowPersonnelList(flowPersonnelListVos);
        //流程历史审核记录
        List<FlowHistoryListVo> flowHistoryListVos = personnelMapper.flowHistoryList(flowId);
        flowDetailVo.setFlowHistoryList(flowHistoryListVos);
        Integer currentCheckType = startFlow.getCurrentCheckType();
        String currentDepartmentName = "";
        if(currentCheckType == 0 && startFlow.getCurrentUserType().equals(adminUser.getUserType())){
            flowDetailVo.setCheckAllow(1);
            GwyDepartment department = departmentMapper.selectById(adminUser.getDepartmentId());
            if(department != null){
                currentDepartmentName = department.getDepartmentName();
            }
        }else if(currentCheckType == 1 && Objects.equals(startFlow.getCurrentDepartmentId(), adminUser.getDepartmentId())){
            flowDetailVo.setCheckAllow(1);
            GwyDepartment department = departmentMapper.selectById(startFlow.getCurrentDepartmentId());
            if(department != null){
                currentDepartmentName = department.getDepartmentName();
            }
        }
        flowDetailVo.setCurrentDepartmentName(currentDepartmentName);
        //发起单位有编辑权限
        if(flowDetailVo.getFlowPos() == 0 && flowDetailVo.getCheckAllow() == 1){
            flowDetailVo.setEditAllow(1);
        }
        return flowDetailVo;
    }

    /**
     * 发起流程
     * @param startFlowDto
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean startFlow(StartFlowDto startFlowDto){
        String flowId = startFlowDto.getFlowId();
        GwyStartFlow startFlowOriginal = new GwyStartFlow();
        if(StringUtils.isNotBlank(flowId)){
            startFlowOriginal = startFlowMapper.selectById(flowId);
            if(startFlowOriginal == null){
                throw new CustomerException(ErrorCode.PARAMS_GET_ERROR,"流程不存在");
            }
        }
        Integer flowType = startFlowDto.getFlowType();
        List<StartFlowPersonnelListDto> startFlowPersonnelList = startFlowDto.getStartFlowPersonnelList();
        //转任是调出单位确认
        if(!Objects.equals(flowType, FlowTypeEnum.ZR.getType()) && CollectionUtils.isEmpty(startFlowPersonnelList)){
            throw new CustomerException(ErrorCode.PARAMS_GET_ERROR,"请选择至少一个人员");
        }
        List<String> attachmentList = startFlowDto.getAttachmentList();
        String departmentId = JwtUtil.getDepartmentId();
        GwyDepartment department = departmentMapper.selectById(departmentId);
        if(department == null){
            throw new CustomerException(ErrorCode.PARAMS_GET_ERROR,"所属机构不存在，请退出重新登录");
        }
        try {
            int result = 0;
            //重新发起直接到上次拒绝的节点
            if(StringUtils.isNotBlank(flowId)){
                startFlowOriginal.setFlowId(flowId);
                startFlowOriginal.setFlowPos(startFlowOriginal.getLastFlowPos());
                startFlowOriginal.setCurrentCheckType(startFlowOriginal.getLastCheckType());
                startFlowOriginal.setCurrentUserType(startFlowOriginal.getLastUserType());
                startFlowOriginal.setCurrentDepartmentId(startFlowOriginal.getLastDepartmentId());
                result = startFlowMapper.updateById(startFlowOriginal);
            }else{
                //获取审核流程信息
                CheckDepartmentListVo departmentListVo = createFlow(flowType, 1, departmentId,  "");
                GwyStartFlow startFlow = new GwyStartFlow();
                startFlow.setCurrentCheckType(departmentListVo.getCurrentCheckType());
                startFlow.setCurrentUserType(departmentListVo.getCurrentUserType());
                String currentDepartmentId = "";
                if(StringUtils.isNotBlank(departmentListVo.getCurrentDepartmentId())){
                    currentDepartmentId = departmentListVo.getCurrentDepartmentId();
                }else{
                    currentDepartmentId = getDepartmentIdByUserType(departmentListVo.getCurrentUserType());
                }
                startFlow.setCurrentDepartmentId(currentDepartmentId);
                startFlow.setLastUserType(departmentListVo.getLastUserType());
                startFlow.setOverStatus(departmentListVo.getLastCheck());
                startFlow.setFlowType(flowType);
                startFlow.setStartDepartmentId(departmentId);
                Date nowTime = new Date();
                startFlow.setStartFlowTime(nowTime);
                startFlow.setLastCheckTime(nowTime);
                startFlow.setFlowPos(1);
                result = startFlowMapper.insert(startFlow);
                flowId = startFlow.getFlowId();
            }
            if(result > 0){
                //记录流程相关人员
                flowPersonnelMapper.delete(new LambdaQueryWrapper<GwyFlowPersonnel>()
                        .eq(GwyFlowPersonnel::getFlowId,flowId));
                if(CollectionUtils.isNotEmpty(startFlowPersonnelList)){
                    for (StartFlowPersonnelListDto personnel : startFlowPersonnelList){
                        GwyFlowPersonnel gwyFlowPersonnel = BeanUtil.copyProperties(personnel, GwyFlowPersonnel.class);
                        gwyFlowPersonnel.setFlowId(flowId);
                        if(StringUtils.isNotBlank(personnel.getDepartmentNo())){
                            GwyDepartment gwyDepartment = departmentMapper.selectOne(new LambdaQueryWrapper<GwyDepartment>()
                                    .eq(GwyDepartment::getDepartmentNo, personnel.getDepartmentNo()).last("limit 1"));
                            if(gwyDepartment != null){
                                gwyFlowPersonnel.setDepartmentId(gwyDepartment.getDepartmentId());
                                gwyFlowPersonnel.setDepartmentName(gwyDepartment.getDepartmentName());
                            }
                        }
                        flowPersonnelMapper.insert(gwyFlowPersonnel);
                    }
                }
                //添加审核记录
                GwyFlowHistory flowHistory = new GwyFlowHistory();
                flowHistory.setHistoryTitle(department.getDepartmentName() + "单位发起");
                flowHistory.setFlowId(flowId);
                flowHistory.setCheckStatus(3);
                int historyInsert = flowHistoryMapper.insert(flowHistory);
                if(historyInsert > 0){
                    //记录流程附件
                    for (String attachment : attachmentList){
                        GwyFlowAttachment flowAttachment = new GwyFlowAttachment();
                        flowAttachment.setAttachmentFile(attachment);
                        flowAttachment.setHistoryId(flowHistory.getHistoryId());
                        flowAttachment.setFlowId(flowId);
                        flowAttachmentMapper.insert(flowAttachment);
                    }
                    //添加提醒
                    GwyFlowNotice flowNotice = new GwyFlowNotice();
                    flowNotice.setFlowId(flowId);
                    flowNotice.setFlowType(flowType);
                    flowNotice.setStartDepartmentId(departmentId);
                    String flowTypeName = FlowTypeEnum.getName(flowType);
                    flowNotice.setNoticeTitle(department.getDepartmentName() + "发起" + flowTypeName + "流程");
                    flowNoticeMapper.insert(flowNotice);
                }
            }
            return result > 0;
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            log.info("startFlow error :" + ExceptionUtils.getMessage(e));
        }
        return false;
    }

    /**
     * 审核流程
     * @param checkFlowDto
     * @return
     */
    public boolean checkFlow(CheckFlowDto checkFlowDto){
        Integer flowStatus = checkFlowDto.getFlowStatus();
        if(flowStatus == 2 && StringUtils.isBlank(checkFlowDto.getRefuseReason())){
            throw new CustomerException(ErrorCode.TOKEN_INVALID,"请输入拒绝理由");
        }
        String loginId = JwtUtil.getLoginId();
        GwyAdminUser adminUser = adminUserMapper.selectById(loginId);
        if(adminUser == null){
            throw new CustomerException(ErrorCode.TOKEN_INVALID,"登录失效，请重新登录");
        }
        GwyDepartment department = departmentMapper.selectById(adminUser.getDepartmentId());
        if(department == null){
            throw new CustomerException(ErrorCode.PARAMS_GET_ERROR,"所属单位不存在");
        }
        String flowId = checkFlowDto.getFlowId();
        GwyStartFlow startFlow = startFlowMapper.selectById(flowId);
        if(startFlow == null){
            throw new CustomerException(ErrorCode.PARAMS_GET_ERROR,"流程不存在");
        }
        Integer currentCheckType = startFlow.getCurrentCheckType();
        String currentDepartmentId = startFlow.getCurrentDepartmentId();
        if(currentCheckType == 0 && !startFlow.getCurrentUserType().equals(adminUser.getUserType())){
            throw new CustomerException(ErrorCode.PARAMS_GET_ERROR,"无权限审批该流程的当前审批节点");
        }else if(currentCheckType == 1 && !Objects.equals(currentDepartmentId, adminUser.getDepartmentId())){
            throw new CustomerException(ErrorCode.PARAMS_GET_ERROR,"无权限审批该流程的当前审批节点");
        }
        try {
            GwyStartFlow gwyStartFlow = new GwyStartFlow();
            gwyStartFlow.setFlowId(flowId);
            gwyStartFlow.setLastCheckTime(new Date());
            //通过审批节点后移
            int update = 0 ;
            int lastCheck = 0;
            if(flowStatus == 1){
                //拒绝重新发起
                if(startFlow.getFlowPos() == 0){
                    GwyStartFlow gwyStartFlowAgain = new GwyStartFlow();
                    gwyStartFlowAgain.setFlowId(flowId);
                    gwyStartFlowAgain.setFlowPos(startFlow.getLastFlowPos());
                    gwyStartFlowAgain.setCurrentCheckType(startFlow.getLastCheckType());
                    gwyStartFlowAgain.setCurrentUserType(startFlow.getLastUserType());
                    gwyStartFlowAgain.setCurrentDepartmentId(startFlow.getLastDepartmentId());
                    update = startFlowMapper.updateById(gwyStartFlowAgain);
                }else{
                    //审批节点后移
                    int flowPos = startFlow.getFlowPos()+1;
                    CheckDepartmentListVo departmentListVo = createFlow(startFlow.getFlowType(), flowPos, startFlow.getStartDepartmentId(), checkFlowDto.getTargetDepartmentId());
                    gwyStartFlow.setCurrentCheckType(departmentListVo.getCurrentCheckType());
                    gwyStartFlow.setCurrentUserType(departmentListVo.getCurrentUserType());
                    String nextDepartmentId = "";
                    if(StringUtils.isNotBlank(departmentListVo.getCurrentDepartmentId())){
                        nextDepartmentId = departmentListVo.getCurrentDepartmentId();
                    }else{
                        nextDepartmentId = getDepartmentIdByUserType(departmentListVo.getCurrentUserType());
                    }
                    gwyStartFlow.setCurrentDepartmentId(nextDepartmentId);
                    gwyStartFlow.setLastUserType(departmentListVo.getLastUserType());
                    gwyStartFlow.setOverStatus(departmentListVo.getLastCheck());
                    gwyStartFlow.setTargetDepartmentId(checkFlowDto.getTargetDepartmentId());
                    //当前审批人变成历史审批人
                    String superDepartmentId = (currentCheckType == 1) ? currentDepartmentId : adminUser.getDepartmentId();
                    gwyStartFlow.setSuperDepartmentId(superDepartmentId);
                    gwyStartFlow.setFlowPos(departmentListVo.getLastCheck() == 1 ? flowPos -1 : flowPos);
                    update = startFlowMapper.updateById(gwyStartFlow);
                    lastCheck = departmentListVo.getLastCheck();
                }
                if(update > 0){
                    //记录流程相关人员
                    List<StartFlowPersonnelListDto> startFlowPersonnelList = checkFlowDto.getStartFlowPersonnelList();
                    if(CollectionUtils.isNotEmpty(startFlowPersonnelList)){
                        for (StartFlowPersonnelListDto personnel : startFlowPersonnelList){
                            GwyFlowPersonnel gwyFlowPersonnel = BeanUtil.copyProperties(personnel, GwyFlowPersonnel.class);
                            gwyFlowPersonnel.setFlowId(startFlow.getFlowId());
                            if(StringUtils.isNotBlank(personnel.getDepartmentNo())){
                                GwyDepartment gwyDepartment = departmentMapper.selectOne(new LambdaQueryWrapper<GwyDepartment>()
                                        .eq(GwyDepartment::getDepartmentNo, personnel.getDepartmentNo()).last("limit 1"));
                                if(gwyDepartment != null){
                                    gwyFlowPersonnel.setDepartmentId(gwyDepartment.getDepartmentId());
                                    gwyFlowPersonnel.setDepartmentName(gwyDepartment.getDepartmentName());
                                }
                            }
                            flowPersonnelMapper.insert(gwyFlowPersonnel);
                        }
                    }
                    //添加审核记录
                    GwyFlowHistory flowHistory = new GwyFlowHistory();
                    flowHistory.setHistoryTitle(department.getDepartmentName() + "单位审核通过");
                    flowHistory.setFlowId(startFlow.getFlowId());
                    flowHistory.setCheckStatus(1);
                    int historyInsert = flowHistoryMapper.insert(flowHistory);
                    if(historyInsert > 0){
                        //记录流程附件
                        List<String> attachmentList = checkFlowDto.getAttachmentList();
                        if(CollectionUtils.isNotEmpty(attachmentList)){
                            for (String attachment : attachmentList){
                                GwyFlowAttachment flowAttachment = new GwyFlowAttachment();
                                flowAttachment.setAttachmentFile(attachment);
                                flowAttachment.setHistoryId(flowHistory.getHistoryId());
                                flowAttachment.setFlowId(startFlow.getFlowId());
                                flowAttachmentMapper.insert(flowAttachment);
                            }
                        }
                        //添加已完成提醒
                        if(lastCheck == 1){
                            GwyFlowNotice flowNotice = new GwyFlowNotice();
                            flowNotice.setFlowId(flowId);
                            flowNotice.setFlowType(startFlow.getFlowType());
                            flowNotice.setStartDepartmentId(startFlow.getStartDepartmentId());
                            flowNotice.setTargetDepartmentId(checkFlowDto.getTargetDepartmentId());
                            String flowTypeName = FlowTypeEnum.getName(startFlow.getFlowType());
                            flowNotice.setNoticeTitle(department.getDepartmentName()+flowTypeName + "流程已完成");
                            flowNoticeMapper.insert(flowNotice);
                        }
                    }
                }
            }else{ //拒绝回滚到发起单位
                gwyStartFlow.setFlowPos(0);
                gwyStartFlow.setSuperDepartmentId("");
                gwyStartFlow.setCurrentCheckType(1);
                gwyStartFlow.setCurrentUserType(0);
                //回到发起单位
                gwyStartFlow.setCurrentDepartmentId(startFlow.getStartDepartmentId());
                gwyStartFlow.setOverStatus(0);
                gwyStartFlow.setLastCheckType(startFlow.getCurrentCheckType());
                gwyStartFlow.setLastDepartmentId(startFlow.getCurrentDepartmentId());
                gwyStartFlow.setLastUserType(startFlow.getCurrentUserType());
                gwyStartFlow.setLastFlowPos(startFlow.getFlowPos());
                update = startFlowMapper.updateById(gwyStartFlow);
                if(update > 0){
                    //添加审核记录
                    GwyFlowHistory flowHistory = new GwyFlowHistory();
                    flowHistory.setHistoryTitle(department.getDepartmentName() + "单位审核拒绝");
                    flowHistory.setFlowId(flowId);
                    flowHistory.setCheckStatus(2);
                    flowHistory.setRefuseReason(checkFlowDto.getRefuseReason());
                    int historyInsert = flowHistoryMapper.insert(flowHistory);
                    if(historyInsert > 0){
                        //记录流程附件
                        List<String> attachmentList = checkFlowDto.getAttachmentList();
                        if(CollectionUtils.isNotEmpty(attachmentList)){
                            for (String attachment : attachmentList){
                                GwyFlowAttachment flowAttachment = new GwyFlowAttachment();
                                flowAttachment.setAttachmentFile(attachment);
                                flowAttachment.setHistoryId(flowHistory.getHistoryId());
                                flowAttachment.setFlowId(startFlow.getFlowId());
                                flowAttachmentMapper.insert(flowAttachment);
                            }
                        }
                    }
                }
            }
            return true;
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            log.info("checkFlow error:" + ExceptionUtils.getMessage(e));
        }
        return false;
    }

    /**
     * 根据发起类型，获取审核部门
     * @param flowType
     * @param flowPos 审核节点位置
     * @param startDepartmentId 发起部门
     * @param targetDepartmentId 转任流程的调出单位
     * @return
     */
    public CheckDepartmentListVo createFlow(Integer flowType, Integer flowPos, String startDepartmentId,String targetDepartmentId){
        CheckDepartmentListVo departmentListVo = new CheckDepartmentListVo();
        departmentListVo.setStartDepartmentId(startDepartmentId);
        departmentListVo.setCurrentCheckType(0);
        departmentListVo.setLastCheck(0);
        departmentListVo.setCurrentUserType(0);
        //录用
        if(Objects.equals(flowType, FlowTypeEnum.LY.getType())){
            if(flowPos == 1){
                departmentListVo.setCurrentUserType(UserTypeEnum.KLC.getType());
            }else if(flowPos == 2){
                departmentListVo.setCurrentUserType(UserTypeEnum.XXC.getType());
            }else if(flowPos == 3){
                departmentListVo.setCurrentDepartmentId(startDepartmentId);
                departmentListVo.setCurrentCheckType(1);
            }else if(flowPos == 4){
                departmentListVo.setCurrentUserType(UserTypeEnum.GZC.getType());
                departmentListVo.setLastUserType(UserTypeEnum.GZC.getType());
            }else{
                departmentListVo.setCurrentUserType(UserTypeEnum.GZC.getType());
                departmentListVo.setLastUserType(UserTypeEnum.GZC.getType());
                departmentListVo.setLastCheck(1);
            }
        }else if(Objects.equals(flowType, FlowTypeEnum.JAZ.getType())){ //军转安置
            if(flowPos == 1){
                departmentListVo.setCurrentUserType(UserTypeEnum.ZHC.getType());
            }else if(flowPos == 2){
                departmentListVo.setCurrentUserType(UserTypeEnum.XXC.getType());
            }else if(flowPos == 3){
                departmentListVo.setCurrentUserType(UserTypeEnum.GZC.getType());
                departmentListVo.setLastUserType(UserTypeEnum.GZC.getType());
            }else{
                departmentListVo.setCurrentUserType(UserTypeEnum.GZC.getType());
                departmentListVo.setLastUserType(UserTypeEnum.GZC.getType());
                departmentListVo.setLastCheck(1);
            }
        }else if(Objects.equals(flowType, FlowTypeEnum.DR.getType())){ //调任
            if(flowPos == 1){
                departmentListVo.setCurrentUserType(UserTypeEnum.KLC.getType());
            }else if(flowPos == 2){
                departmentListVo.setCurrentUserType(UserTypeEnum.ZHC.getType());
            }else if(flowPos == 3){
                departmentListVo.setCurrentUserType(UserTypeEnum.XXC.getType());
            }else if(flowPos == 4){
                departmentListVo.setCurrentUserType(UserTypeEnum.GZC.getType());
                departmentListVo.setLastUserType(UserTypeEnum.GZC.getType());
            }else{
                departmentListVo.setCurrentUserType(UserTypeEnum.GZC.getType());
                departmentListVo.setLastUserType(UserTypeEnum.GZC.getType());
                departmentListVo.setLastCheck(1);
            }
        }else if(Objects.equals(flowType, FlowTypeEnum.TB.getType())){ //提拔
            if(flowPos == 1){
                departmentListVo.setCurrentUserType(UserTypeEnum.GBC.getType());
            }else if(flowPos == 2){
                departmentListVo.setCurrentDepartmentId(startDepartmentId);
                departmentListVo.setCurrentCheckType(1);
            }else if(flowPos == 3){
                departmentListVo.setCurrentUserType(UserTypeEnum.ZHC.getType());
            }else if(flowPos == 4){
                departmentListVo.setCurrentUserType(UserTypeEnum.XXC.getType());
            }else if(flowPos == 5){
                departmentListVo.setCurrentUserType(UserTypeEnum.GZC.getType());
                departmentListVo.setLastUserType(UserTypeEnum.GZC.getType());
            }else{
                departmentListVo.setCurrentUserType(UserTypeEnum.GZC.getType());
                departmentListVo.setLastUserType(UserTypeEnum.GZC.getType());
                departmentListVo.setLastCheck(1);
            }
        }else if(Objects.equals(flowType, FlowTypeEnum.JL.getType())){ //职务变动(交流)
            if(flowPos == 1){
                departmentListVo.setCurrentUserType(UserTypeEnum.GBC.getType());
            }else if(flowPos == 2){
                departmentListVo.setCurrentDepartmentId(startDepartmentId);
                departmentListVo.setCurrentCheckType(1);
            }else if(flowPos == 3){
                departmentListVo.setCurrentUserType(UserTypeEnum.ZHC.getType());
            }else if(flowPos == 4){
                departmentListVo.setCurrentUserType(UserTypeEnum.XXC.getType());
                departmentListVo.setLastUserType(UserTypeEnum.XXC.getType());
            }else{
                departmentListVo.setCurrentUserType(UserTypeEnum.XXC.getType());
                departmentListVo.setLastUserType(UserTypeEnum.XXC.getType());
                departmentListVo.setLastCheck(1);
            }
        }else if(Objects.equals(flowType, FlowTypeEnum.ZJB.getType())){ //职级变动
            if(flowPos == 1){
                departmentListVo.setCurrentUserType(UserTypeEnum.ZHC.getType());
            }else if(flowPos == 2){
                departmentListVo.setCurrentDepartmentId(startDepartmentId);
                departmentListVo.setCurrentCheckType(1);
            }else if(flowPos == 3){
                departmentListVo.setCurrentUserType(UserTypeEnum.ZHC.getType());
            }else if(flowPos == 4){
                departmentListVo.setCurrentUserType(UserTypeEnum.XXC.getType());
            }else if(flowPos == 5){
                departmentListVo.setCurrentUserType(UserTypeEnum.GZC.getType());
                departmentListVo.setLastUserType(UserTypeEnum.GZC.getType());
            }else{
                departmentListVo.setCurrentUserType(UserTypeEnum.GZC.getType());
                departmentListVo.setLastUserType(UserTypeEnum.GZC.getType());
                departmentListVo.setLastCheck(1);
            }
        }else if(Objects.equals(flowType, FlowTypeEnum.DJ.getType())){ // 公务员登记
            if(flowPos == 1){
                departmentListVo.setCurrentUserType(UserTypeEnum.ZHC.getType());
            }else if(flowPos == 2){
                departmentListVo.setCurrentUserType(UserTypeEnum.KLC.getType());
            }else if(flowPos == 3){
                departmentListVo.setCurrentUserType(UserTypeEnum.XXC.getType());
            }else if(flowPos == 4){
                departmentListVo.setCurrentUserType(UserTypeEnum.GZC.getType());
                departmentListVo.setLastUserType(UserTypeEnum.GZC.getType());
            }else{
                departmentListVo.setCurrentUserType(UserTypeEnum.GZC.getType());
                departmentListVo.setLastUserType(UserTypeEnum.GZC.getType());
                departmentListVo.setLastCheck(1);
            }
        }else if(Objects.equals(flowType, FlowTypeEnum.ZR.getType())){ //转任
            if(flowPos == 1){
                departmentListVo.setCurrentUserType(UserTypeEnum.ZHC.getType());
            }else if(flowPos == 2){
                departmentListVo.setCurrentDepartmentId(targetDepartmentId);
                departmentListVo.setCurrentCheckType(1);
            }else if(flowPos == 3){
                departmentListVo.setCurrentUserType(UserTypeEnum.ZHC.getType());
            }else if(flowPos == 4){
                departmentListVo.setCurrentDepartmentId(targetDepartmentId);
                departmentListVo.setCurrentCheckType(1);
            }else if(flowPos == 5){
                departmentListVo.setCurrentUserType(UserTypeEnum.ZHC.getType());
            }else if(flowPos == 6){
                departmentListVo.setCurrentDepartmentId(startDepartmentId);
                departmentListVo.setCurrentCheckType(1);
            }else if(flowPos == 7){
                departmentListVo.setCurrentUserType(UserTypeEnum.ZHC.getType());
            }else if(flowPos == 8){
                departmentListVo.setCurrentUserType(UserTypeEnum.XXC.getType());
            }else if(flowPos == 9){
                departmentListVo.setCurrentUserType(UserTypeEnum.GZC.getType());
                departmentListVo.setLastUserType(UserTypeEnum.GZC.getType());
            }else{
                departmentListVo.setCurrentUserType(UserTypeEnum.GZC.getType());
                departmentListVo.setLastUserType(UserTypeEnum.GZC.getType());
                departmentListVo.setLastCheck(1);
            }
        }else if(Objects.equals(flowType, FlowTypeEnum.QX.getType())){ //退休、调出、辞职、辞退、开除
            if(flowPos == 1){
                departmentListVo.setCurrentUserType(UserTypeEnum.KLC.getType());
            }else if(flowPos == 2){
                departmentListVo.setCurrentUserType(UserTypeEnum.XXC.getType());
            }else if(flowPos == 3){
                departmentListVo.setCurrentUserType(UserTypeEnum.GZC.getType());
                departmentListVo.setLastUserType(UserTypeEnum.GZC.getType());
            }else{
                departmentListVo.setCurrentUserType(UserTypeEnum.GZC.getType());
                departmentListVo.setLastUserType(UserTypeEnum.GZC.getType());
                departmentListVo.setLastCheck(1);
            }
        }else if(Objects.equals(flowType, FlowTypeEnum.TX.getType()) ||
                Objects.equals(flowType, FlowTypeEnum.DC.getType()) ||
                Objects.equals(flowType, FlowTypeEnum.CZ.getType()) ||
                Objects.equals(flowType, FlowTypeEnum.CT.getType()) ||
                Objects.equals(flowType, FlowTypeEnum.KC.getType())){ //军转安置
            if(flowPos == 1){
                departmentListVo.setCurrentUserType(UserTypeEnum.ZHC.getType());
            }else if(flowPos == 2){
                departmentListVo.setCurrentUserType(UserTypeEnum.XXC.getType());
            }else if(flowPos == 3){
                departmentListVo.setCurrentUserType(UserTypeEnum.GZC.getType());
                departmentListVo.setLastUserType(UserTypeEnum.GZC.getType());
            }else{
                departmentListVo.setCurrentUserType(UserTypeEnum.GZC.getType());
                departmentListVo.setLastUserType(UserTypeEnum.GZC.getType());
                departmentListVo.setLastCheck(1);
            }
        }
        return departmentListVo;
    }

    /**
     * 通过账号类型获取所属单位
     * @param userType
     * @return
     */
    public String getDepartmentIdByUserType(Integer userType){
        GwyAdminUser adminUser = adminUserMapper.selectOne(new LambdaQueryWrapper<GwyAdminUser>()
                .eq(GwyAdminUser::getUserType, userType)
                .last("limit 1"));
        if(adminUser != null){
            String departmentId = adminUser.getDepartmentId();
            GwyDepartment department = departmentMapper.selectById(departmentId);
            return department == null ? "" : department.getDepartmentId();
        }
        return "";
    }

    /**
     * 通过审批类型，获取审批角色
     * @param flowType
     * @return
     */
    public List<Integer> getUserTypeByFlowType(Integer flowType){
        List<Integer> userTypeList = new ArrayList<>();
        //录用
        if(Objects.equals(flowType, FlowTypeEnum.LY.getType())){
            userTypeList = Arrays.asList(UserTypeEnum.KLC.getType(), UserTypeEnum.XXC.getType(), UserTypeEnum.GZC.getType());

        }else if(Objects.equals(flowType, FlowTypeEnum.JAZ.getType())){ //军转安置
            userTypeList = Arrays.asList(UserTypeEnum.ZHC.getType(), UserTypeEnum.XXC.getType(), UserTypeEnum.GZC.getType());

        }else if(Objects.equals(flowType, FlowTypeEnum.DR.getType())){ //调任
            userTypeList = Arrays.asList(UserTypeEnum.KLC.getType(), UserTypeEnum.ZHC.getType(), UserTypeEnum.XXC.getType(),UserTypeEnum.GZC.getType());

        }else if(Objects.equals(flowType, FlowTypeEnum.TB.getType())){ //提拔
            userTypeList = Arrays.asList(UserTypeEnum.GBC.getType(), UserTypeEnum.ZHC.getType(), UserTypeEnum.XXC.getType(),UserTypeEnum.GZC.getType());

        }else if(Objects.equals(flowType, FlowTypeEnum.JL.getType())){ //职务变动(交流)
            userTypeList = Arrays.asList(UserTypeEnum.GBC.getType(), UserTypeEnum.ZHC.getType(), UserTypeEnum.XXC.getType());

        }else if(Objects.equals(flowType, FlowTypeEnum.ZJB.getType())){ //职级变动
            userTypeList = Arrays.asList(UserTypeEnum.ZHC.getType(),  UserTypeEnum.XXC.getType(),UserTypeEnum.GZC.getType());

        }else if(Objects.equals(flowType, FlowTypeEnum.DJ.getType())){ // 公务员登记
            userTypeList = Arrays.asList(UserTypeEnum.ZHC.getType(), UserTypeEnum.KLC.getType(), UserTypeEnum.XXC.getType(),UserTypeEnum.GZC.getType());

        }else if(Objects.equals(flowType, FlowTypeEnum.ZR.getType())){ //转任
            userTypeList = Arrays.asList(UserTypeEnum.ZHC.getType(), UserTypeEnum.XXC.getType(),UserTypeEnum.GZC.getType());

        }else if(Objects.equals(flowType, FlowTypeEnum.QX.getType())){ //取消录用
            userTypeList = Arrays.asList(UserTypeEnum.KLC.getType(), UserTypeEnum.XXC.getType(), UserTypeEnum.GZC.getType());

        }else if(Objects.equals(flowType, FlowTypeEnum.TX.getType()) ||
                Objects.equals(flowType, FlowTypeEnum.DC.getType()) ||
                Objects.equals(flowType, FlowTypeEnum.CZ.getType()) ||
                Objects.equals(flowType, FlowTypeEnum.CT.getType()) ||
                Objects.equals(flowType, FlowTypeEnum.KC.getType())){ //退休、调出、辞职、辞退、开除
            userTypeList = Arrays.asList(UserTypeEnum.ZHC.getType(), UserTypeEnum.XXC.getType(), UserTypeEnum.GZC.getType());
        }
        return userTypeList;
    }

    /**
     * 根据用户类型获取有查看权限流程类型
     * @param userType
     * @return
     */
    public List<Integer> getFlowTypeByUserType(Integer userType){
        List<Integer> flowTypeList = new ArrayList<>();
        if(Objects.equals(userType, UserTypeEnum.KLC.getType())){
            flowTypeList = Arrays.asList(FlowTypeEnum.LY.getType(), FlowTypeEnum.DR.getType(), FlowTypeEnum.DJ.getType(),FlowTypeEnum.QX.getType());

        }else if(Objects.equals(userType, UserTypeEnum.GZC.getType())){
            flowTypeList = Arrays.asList(FlowTypeEnum.LY.getType(),FlowTypeEnum.JAZ.getType(), FlowTypeEnum.DR.getType(), FlowTypeEnum.TB.getType(),
                    FlowTypeEnum.ZJB.getType(), FlowTypeEnum.ZR.getType(),FlowTypeEnum.DJ.getType(),FlowTypeEnum.TX.getType()
                    ,FlowTypeEnum.DC.getType(),FlowTypeEnum.CZ.getType(),FlowTypeEnum.CT.getType(),FlowTypeEnum.KC.getType(),FlowTypeEnum.QX.getType());

        }else if(Objects.equals(userType, UserTypeEnum.XXC.getType())){
            flowTypeList = Arrays.asList(FlowTypeEnum.LY.getType(),FlowTypeEnum.JAZ.getType(), FlowTypeEnum.DR.getType(), FlowTypeEnum.TB.getType(),
                    FlowTypeEnum.JL.getType(),FlowTypeEnum.ZJB.getType(), FlowTypeEnum.ZR.getType(),FlowTypeEnum.DJ.getType(),FlowTypeEnum.TX.getType()
                    ,FlowTypeEnum.DC.getType(),FlowTypeEnum.CZ.getType(),FlowTypeEnum.CT.getType(),FlowTypeEnum.KC.getType(),FlowTypeEnum.QX.getType());

        }else if(Objects.equals(userType, UserTypeEnum.ZHC.getType())){
            flowTypeList = Arrays.asList(FlowTypeEnum.JAZ.getType(), FlowTypeEnum.DR.getType(), FlowTypeEnum.TB.getType(),
                    FlowTypeEnum.JL.getType(),FlowTypeEnum.ZJB.getType(), FlowTypeEnum.ZR.getType(),FlowTypeEnum.DJ.getType(),FlowTypeEnum.TX.getType()
                    ,FlowTypeEnum.DC.getType(),FlowTypeEnum.CZ.getType(),FlowTypeEnum.CT.getType(),FlowTypeEnum.KC.getType());

        }else if(Objects.equals(userType, UserTypeEnum.GBC.getType())){
            flowTypeList = Arrays.asList(FlowTypeEnum.JL.getType(), FlowTypeEnum.TB.getType());

        }
        if(CollectionUtils.isEmpty(flowTypeList)){
            return Collections.singletonList(0);
        }
        return flowTypeList;
    }
}
